<?php
/**
 * Created by PhpStorm.
 * User: MRPHAN
 * Date: 9/21/2014
 * Time: 8:37 PM
 */
namespace Backend\Modules\Video\Controllers;
 use Backend\Modules\Video\Forms\FormCreateArtist;
 use Backend\Modules\Video\Models\Artist ;
 use Backend\Modules\Video\Models\VideoArtistLink as VideoArtistLink;
 use Phalcon\Paginator\Adapter\Model as Paginator;
 use Phalcon\Mvc\Model\Resultset;
 use Library\Pagination\Pager as Pager;
 use Common\Library\Common as LibraryCommon;
 use Library\Upload\UploadFile as UploadFile;

 class ArtistController extends \BackendController{
     public function indexAction(){

         $currentPage = abs($this->request->getQuery('page', 'int', 1));
         $artists = Artist::find();

         if ($currentPage == 0) {
             $currentPage = 1;
         }

         $pager = new Pager(
             new Paginator(array(
                 'data'  => $artists,
                 'limit' => 20,
                 'page'  => $currentPage,
             )),
             array(
                 'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                 'rangeLength' => 5,
                 'urlMask'     => '?page={%page_number}',
             )
         );

         $this->view->pager = $pager;
     }

     public function  createAction(){
         $this->assets
             ->addCss('vendor/bootstrap/dist/css/bootstrap-datepicker.css')
             ->addJs('vendor/bootstrap/dist/js/bootstrap-datepicker.js')
             ->addJs('vendor/backend/js/video/artist.js')
             ->addJs('vendor/editor/ckeditor/ckeditor.js')
             ->addJs('vendor/typeahead/typeahead.bundle.js')
             ->addJs('vendor/bootstrap-tagsinput/dist/bootstrap-tagsinput.js')
             ->addJs('vendor/bootstrap-tagsinput/dist/bootstrap-tagsinput-angular.js');

         $form = new FormCreateArtist();

         if($this->request->getPost())
         {
            if($form->isValid($this->request->getPost()) !=  false)
            {
                //Check if the user has uploaded files
                if ($this->request->hasFiles() == true) {
                    //Print the real file names and their sizes
                    foreach ($this->request->getUploadedFiles() as $file){
                        echo $file->getName(), " ", $file->getSize(), "\n";
                    }
                }
                $artist = new Artist();
                $artist->assign(array(
                    'artist_type'      => $this->request->getPost('artist_type'),
                    'name'             => $this->request->getPost('name'),
                    'national_id'      => $this->request->getPost('national_id'),
                    'gender'           => $this->request->getPost('gender'),
                    'info'             => $this->request->getPost('info'),
                    'bod'              => date( 'Y-m-d', strtotime( $this->request->getPost('bod') ) ),
                    'short_name'       => LibraryCommon::stripTag($this->request->getPost('name'))
                ));
                if($artist->save())
                {
                    return $this->dispatcher->forward(array(
                        'controller' => 'artist',
                        'action' => 'index'
                    ));
                }
                else
                {
                    $this->flash->error($artist->getMessages());
                }
            }
         }

         $this->view->form = $form;
     }

     public function editAction($id){
         $this->assets
             ->addCss('layouts/skins/backend/defaults/css/bootstrap-datepicker.css');
         $this->assets
             ->addJs('layouts/skins/backend/defaults/js/bootstrap-datepicker.js');
         $this->assets
             ->addJs('vendor/backend/js/artist.js');

         $artist = $this->getFindFirstById($id);
         if(!$artist)
         {
             $this->flash->error("Arist was not found");
             return $this->dispatcher->forward(array(
                 'action' => 'index'
             ));
         }
         $form = new FormCreateArtist($artist , array('edit' => true));

         if($this->request->getPost())
         {
             if($form->isValid($this->request->getPost()) != false)
             {
                 //Check if the user has uploaded files
                 if ($this->request->hasFiles() == true) {
                     $uploadFile = new UploadFile();
                     $uploadFile->doUploadFtp(array('folder_upload' => 'files' , 'object_id' => 0 , 'image_id' => 0 , 'object_type' => 'artist' , 'type' => 'M' , 'position' => 0) , LibraryCommon::listThumbnailOptionA());

                     //Print the real file names and their sizes
                     /*foreach ($this->request->getUploadedFiles() as $file){
                         echo $file->getName(), " ", $file->getSize(), "\n";
                     }*/
                     die();
                 }

                 $artist->assign(array(
                     'artist_type'      => $this->request->getPost('artist_type'),
                     'name'             => $this->request->getPost('name'),
                     'national_id'      => $this->request->getPost('national_id'),
                     'gender'           => $this->request->getPost('gender'),
                     'info'             => $this->request->getPost('info'),
                     'bod'              => $this->request->getPost('bod'),
                     'short_name'       => LibraryCommon::stripTag($this->request->getPost('name'))
                 ));

                 if($artist->save())
                 {
                     return $this->dispatcher->forward(array(
                         'action' => 'index'
                     ));
                 }
                 $this->flash->error($artist->getMessages());
             }
         }
         $this->view->form = $form;
     }
     public function deleteAction($id)
     {
         $artist = $this->getFindFirstById($id);
         if(!$artist)
         {
             $this->flash->error("Video was not found");
             return $this->dispatcher->forward(array(
                 'action' => 'index'
             ));
         }
         if(!$artist->delete())
         {
             $this->flash->error($artist->getMessages());
         }
         else
         {
             $this->flash->success("Artist was deleted");
         }

         return $this->dispatcher->forward(array(
             'action' => 'index'
         ));
     }
     public  function getFindFirstById($id){
         return Artist::findFirstById($id);
     }

     public function getAction()
     {
         $this->view->disable();
         $currentPage = abs($this->request->getQuery('page', 'int', 1));
         $artists = Artist::find();

         if ($currentPage == 0) {
             $currentPage = 1;
         }

         $pager = new Pager(
             new Paginator(array(
                 'data'  => $artists,
                 'limit' => 2,
                 'page'  => $currentPage,
             )),
             array(
                 // We will use Bootstrap framework styles
                 'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                 // Range window will be 5 pages
                 'rangeLength' => 5,
                 // Just a string with URL mask
                 'urlMask'     => 'http://localhost/javhd/javhd-admin/video/artist?page={%page_number}',
             )
         );

         /*foreach($pager->getItems() as $item)
         {
            var_dump($item->getName());
         }*/
         //die();
         //die(print_r($artists->toArray()));
        $json['artists'] = $pager->getItems();
        $json['getLayout'] = $pager->getLayout();
        $json['success'] = true;
         //echo json_encode($artists->toArray());
         //Create a response instance
        $response = new \Phalcon\Http\Response();

        //Set the content of the response
        $response->setContent(json_encode($json));

        //Return the response
        return $response;
     }

     public function searchAction()
     {
         $this->view->disable();
         $currentPage = abs($this->request->getQuery('page', 'int', 1));
         $artists = Artist::find(array(
             "gender = 0"
         ));

         if ($currentPage == 0) {
             $currentPage = 1;
         }

         $pager = new Pager(
             new Paginator(array(
                 'data'  => $artists,
                 'limit' => 2,
                 'page'  => $currentPage,
             )),
             array(
                 // We will use Bootstrap framework styles
                 'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                 // Range window will be 5 pages
                 'rangeLength' => 5,
                 // Just a string with URL mask
                 'urlMask'     => 'http://localhost/javhd/javhd-admin/video/artist?page={%page_number}',
             )
         );

         $json['artists'] = $artists->toArray();//$pager->getItems();
         $json['getLayout'] = $pager->getLayout();
         $json['success'] = true;
         //echo json_encode($artists->toArray());
         //Create a response instance
         $response = new \Phalcon\Http\Response();

         //Set the content of the response
         $response->setContent(json_encode($json));

         //Return the response
         return $response;
     }

 }
